package com.zmn.oms.services.impl.log;

import com.zmn.common.constant.StatusConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.model.entity.log.EntrustOrderSyncLog;
import com.zmn.oms.persistence.interfaces.log.EntrustOrderSyncLogDao;
import com.zmn.oms.services.interfaces.log.EntrustOrderSyncLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2020/01/02 16:52
 */
@Service("entrustOrderSyncLogService")
public class EntrustOrderSyncLogServiceImpl implements EntrustOrderSyncLogService {

    @Resource
    private EntrustOrderSyncLogDao entrustOrderSyncLogDao;

    @Override
    public EntrustOrderSyncLog findByKey(Long logId) {
        if (logId == null) {
            return null;
        }
        return entrustOrderSyncLogDao.findByKey(logId);
    }

    @Override
    public List<EntrustOrderSyncLog> listByEntrustOrderId(Long entrustOrderId) {
        if (entrustOrderId == null) {
            return null;
        }

        List<EntrustOrderSyncLog> entrustOrderSyncLogs = entrustOrderSyncLogDao.ListByEntrustOrderId(entrustOrderId);
        for (EntrustOrderSyncLog item : entrustOrderSyncLogs) {
            item.setOperateTypeName(OrderStatusConsts.getOrderStatusName(item.getOperateType()));
            item.setBeforeStatusName(getEntrustOrderStatusName(item.getBeforeStatus()));
            item.setAfterStatusName(getEntrustOrderStatusName(item.getAfterStatus()));
            item.setResultName(StatusConsts.STATUS_SUCCESS == item.getResult() ? "成功" : "失败");
        }

        return entrustOrderSyncLogs;
    }

    @Override
    public int insertEntrustOrderSyncLog(EntrustOrderSyncLog entrustOrderSyncLog) {
        if (entrustOrderSyncLog == null) {
            return 0;
        }
        return entrustOrderSyncLogDao.insertEntrustOrderSyncLog(entrustOrderSyncLog);
    }

    private String getEntrustOrderStatusName(int status) {
        switch (status) {
            case OrderStatusConsts.ORDER_STATUS_INPUT:
                return "已录入";
            case OrderStatusConsts.ORDER_STATUS_CONFIRM:
                return "已确认";
            case OrderStatusConsts.ORDER_STATUS_DISTRIBUTE:
                return "已派单";
            case OrderStatusConsts.ORDER_STATUS_TAKE:
                return "已领单";
            case OrderStatusConsts.ORDER_STATUS_VISIT:
                return "已上门";
            case OrderStatusConsts.ORDER_STATUS_COMPLETE:
                return "已完成";
            case OrderStatusConsts.ORDER_STATUS_CANCEL:
                return "已取消";
            default:
                return "未知";
        }
    }
}
